<?php

class halo_HttpRequest {

    protected $method;

    protected $queryParams;

    protected $postParams;

    protected $postData;

    protected $fileData;

    protected $env;

    protected $attributes = array();

    protected $urlParams = array();

    protected $scriptPathRoot;

    protected $requestedUrl;

    public function __construct($method, array $queryParams = null, array $postParams = null, array $fileData = null, array $env = null) {

        $haloUrl = null;
        
        if ( isset($queryParams['__haloUrl']) ) {
            $haloUrl = $queryParams['__haloUrl'];
            unset($queryParams['__haloUrl']);
        }

        $this->requestedUrl = $haloUrl;

        $this->method = $method;

        $this->queryParams = $queryParams;

        $this->postParams = $postParams;

        $this->fileData = $fileData;

        if ( $env === null ) {
            $env = array();
        }

        if ( isset($env['REQUEST_URI']) ) {
            $requestUri = $env['REQUEST_URI'];
            $this->scriptPathRoot = $env['SCRIPT_PATH_ROOT'] = substr($requestUri, 0, strpos($requestUri, $this->requestedUrl));
        } else {
            $this->scriptPathRoot = $env['SCRIPT_PATH_ROOT'] = null;
        }

        $this->env = $env;

        $_SERVER = $this->env;

    }
    public function getQueryParams() {
        return $this->queryParams;
    }
    public function getQueryParam($paramName) {
        return $this->queryParamExists($paramName) ? $this->queryParams[$paramName] : null;
    }
    public function queryParamExists($paramName) {
        return array_key_exists($paramName, $this->queryParams) ? true : false;
    }
    public function getPostParams() {
        return $this->postParams;
    }
    public function getPostParam($paramName) {
        return $this->postParamExists($paramName) ? $this->postParams[$paramName] : null;
    }
    public function postParamExists($paramName) {
        return array_key_exists($paramName, $this->postParams) ? true : false;
    }
    public function getFileParams() {
        return $this->fileData;
    }
    public function getFileParam($paramName, $key = null) {
        $param = $this->fileParamExists($paramName) ? $this->fileData[$paramName] : null;
        if ( $key === null ) {
            return $param;
        } else {
            return array_key_exists($key, $param) ? $param[$key] : null;
        }
    }
    public function fileParamExists($paramName) {
        return array_key_exists($paramName, $this->fileData) ? true : false;
    }
    public function getAttributeKeys() {
        return array_keys($this->attributes);
    }
    public function setAttribute($key, $value) {
        $this->attributes[$key] = $value;
    }
    public function getAttribute($key) {
        return array_key_exists($key, $this->attributes) ? $this->attributes[$key] : null;
    }
    public function unsetAttribute($key) {
        unset($this->attributes[$key]);
    }
    protected function isMethod($methodName) {
        return strtoupper($methodName) == strtoupper($this->method);
    }
    public function isGetMethod() {
        return $this->isMethod('GET');
    }
    public function isPostMethod() {
        return $this->isMethod('POST');
    }
    public function getUrlParams() {
        return $this->urlParams;
    }
    public function getUrlParamNames() {
        return array_keys($this->urlParams);
    }
    public function setUrlParam($paramName, $paramValue = null) {
        $this->urlParams[$paramName] = $paramValue;
    }
    public function getUrlParam($paramName) {
        return array_key_exists($paramName, $this->urlParams) ? $this->urlParams[$paramName] : null;
    }
    public function urlParamExists($paramName) {
        return array_key_exists($paramName, $this->urlParams);
    }
    public function getRequestedUrl() {
        return $this->requestedUrl;
    }
    public function getScriptPathRoot() {
        return $this->scriptPathRoot;
    }
    public function getEnv($key) {
        return $this->env[$key];
    }

}

?>
